package com.itheima.reggie.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加菜品或者套餐至购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart:===>{}",shoppingCart);
        //设置当前用户
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        //加入的为菜品
        if(dishId != null){
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //加入的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart setmealOrDish = shoppingCartService.getOne(wrapper);
        if(setmealOrDish != null) {
            //套餐或者菜品已存在在其基础上+1
            Integer number = setmealOrDish.getNumber();
            setmealOrDish.setNumber(number + 1);
            shoppingCartService.updateById(setmealOrDish);
        }else {
            //不存在，执行新增添加操作
            shoppingCartService.save(shoppingCart);
            setmealOrDish = shoppingCart;
        }
        return R.success(setmealOrDish);
    }


    /**
     * 查看购物车数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCartList);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("已清空购物车");
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart:===>{}",shoppingCart);
        //设置当前用户
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        //删减的为菜品
        if(dishId != null){
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //删减的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart setmealOrDish = shoppingCartService.getOne(wrapper);
        //判断购物车中该套餐或者菜品的数量然后进行删减
        if(setmealOrDish.getNumber() > 1) {
            //套餐或者菜品已存在在其基础上+1
            Integer number = setmealOrDish.getNumber();
            setmealOrDish.setNumber(number - 1);
            shoppingCartService.updateById(setmealOrDish);
        }else if(setmealOrDish.getNumber() == 1) {
            //不存在，执行新增添加操作
            shoppingCartService.remove(wrapper);
            setmealOrDish = shoppingCart;
        }
        return R.success(setmealOrDish);

    }

}
